#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>

int main(int argc, char *argv[]) 
{
    int fd =open("test.txt",O_RDONLY);
    if(fd==-1)
    {
        perror("打开test1.txt失败\n");
        exit(EXIT_FAILURE);
    }

    int fd2 = open("test2.txt",O_WRONLY|O_CREAT|O_TRUNC,0644);
    if(fd2==-1)
    {
        perror("打开test2.txt失败\n");
        exit(EXIT_FAILURE);
    }

    char buf[1024];
    ssize_t read_bytes;
    while ((read_bytes = read(fd,buf,sizeof(buf)))>0)
    {
        write(fd2,buf,read_bytes);
        write(STDOUT_FILENO,buf,read_bytes);
    }

    if  (read_bytes==-1)
    {
        perror("读取文件失败\n");
        close(fd);
        close(fd2);
        exit(EXIT_FAILURE);
    }

    close(fd);
    close(fd2);
    return 0;
    
}   